package com.edu.storage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.edu.storage.entity.TStorage;
import com.edu.storage.mapper.TStorageMapper;
import com.edu.storage.service.ITStorageService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Optional;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author dingchengyu
 * @since 2022-05-13
 */
@Service
@Slf4j
public class TStorageServiceImpl extends ServiceImpl<TStorageMapper, TStorage> implements ITStorageService {

    @Override
    public Long selectPrice(String commodityCode) {
        QueryWrapper<TStorage> wapper = new QueryWrapper<>();
        wapper.eq("commodity_code",commodityCode);
        return Optional.of(baseMapper.selectOne(wapper).getPrice()).orElse(null);
    }

    /**
     * 扣减库存
     *
     * @param commodityCode 商品编码
     * @param count         数量
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deductStorage(String commodityCode, int count) {
        log.info("[库存服务]>------>扣减库存开始");
        baseMapper.deductStorage(commodityCode, count);
        log.info("[库存服务]>------>扣减库存结束");
        return count;
    }
}
